using System;
using System.Net;
using System.Net.Sockets;
using System.Collections.Specialized;

namespace Publish.Services
{
    public class SystemService
    {
        /// <summary>  
        /// 获取当前网卡IP地址  
        /// </summary>  
        /// <returns></returns>  
        public static String GetLocalIpv4()
        {
            //事先不知道ip的个数，数组长度未知，因此用StringCollection储存
            var localIPs = Dns.GetHostAddresses(Dns.GetHostName());
            var ipCollection = new StringCollection();
            foreach (IPAddress ip in localIPs)
            {
                //根据AddressFamily判断是否为ipv4,如果是InterNetWork则为ipv6
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                    ipCollection.Add(ip.ToString());
            }
            var ipArray = new String[ipCollection.Count];
            ipCollection.CopyTo(ipArray, 0);

            return String.Join(",", ipArray);//返回IPv4字符串
        }

        /// <summary>
        /// 获取本机的IP列表 为考虑到有些主机装有多网卡 所以将会获取所有的网卡
        /// </summary>
        /// <returns>返回IP信息列表</returns>
        private static IPAddress[] GetLocalIpList()
        {
            String hostName = Dns.GetHostName();
            IPHostEntry ipEntry = Dns.GetHostEntry(hostName);
            return ipEntry.AddressList;
        }
    }
}
